import sqlite3

DATA_PATH = "./x_demo.db"
TABLE_NAME = "users"


def query_all():
    with sqlite3.connect(DATA_PATH) as cnn:
        cur = cnn.cursor()
        cur.execute(
            f"""
            SELECT * FROM {TABLE_NAME}
            """
        )
        rows = cur.fetchall()
    for row in rows:
        print(row)
    # result = "-".join(rows)
    return rows


def initialize_tabel(table_name=TABLE_NAME):
    with sqlite3.connect(DATA_PATH) as cnn:
        cur = cnn.cursor()
        cur.execute(
            f"""
            CREATE TABLE IF NOT EXISTS {TABLE_NAME} (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            age INTEGER)"""
        )
        cnn.commit()


def insert_data(name: str, age: int, table_name=TABLE_NAME):
    with sqlite3.connect(DATA_PATH) as cnn:
        cur = cnn.cursor()
        cur.execute(
            f"""
            INSERT INTO {TABLE_NAME} (name, age) VALUES (?, ?)""",
            (name, age),
        )
        cnn.commit()


if __name__ == "__main__":
    mock_persons = [["john", 32], ["jame", 23]]
    initialize_tabel()
    for p in mock_persons:
        insert_data(p[0], p[1])
    query_all()
